﻿namespace H2.Streams.Sections
{
    using H2.Streams;
    using System;

    public class LightMap
    {
        private int __sbspInfoIndex;
        private Palette[] _palettes;
        private MapStream Map;
        internal int StartOffset;

        internal LightMap(MapStream map, int sbspInfoIndex)
        {
            this.Map = map;
            this.__sbspInfoIndex = sbspInfoIndex;
        }

        internal void CreatePaletteCache()
        {
            this.Map.ReadReflexiveAt(this.StartOffset + 0x80, this.Map.Sbsp[this.SbspInfoIndex, Tags.SearchType.Index].Magic);
            this.Map.Position += 8L;
            int num = this.Map.ReadInt32();
            this.Palettes = new Palette[num];
            for (int i = 0; i < num; i++)
            {
                this.Palettes[i] = new Palette(this.Map, ((int) this.Map.Position) + (i * 0x400));
            }
        }

        public int ID
        {
            get
            {
                return this.Map.ReadInt32At(this.Map.Sbsp.SbspInfoCache[this.SbspInfoIndex].StartOffset + 0x1c, false);
            }
            set
            {
                this.Map.WriteAt(this.Map.Sbsp.SbspInfoCache[this.SbspInfoIndex].StartOffset + 0x1c, value, false);
            }
        }

        public int LightMapBitmapID
        {
            get
            {
                long position = this.Map.Position;
                this.Map.ReadReflexiveAt(this.StartOffset + 0x80, this.Map.Sbsp[this.SbspInfoIndex, Tags.SearchType.Index].Magic);
                this.Map.Position += 0x1cL;
                int num2 = this.Map.ReadInt32();
                this.Map.Position = position;
                return num2;
            }
            set
            {
                long position = this.Map.Position;
                this.Map.ReadReflexiveAt(this.StartOffset + 0x80, this.Map.Sbsp[this.SbspInfoIndex, Tags.SearchType.Index].Magic);
                this.Map.Position += 0x1cL;
                this.Map.Write(value);
                this.Map.Position = position;
            }
        }

        public int Offset
        {
            get
            {
                return (this.Map.ReadInt32At(this.Map.Sbsp.SbspInfoCache[this.SbspInfoIndex].Offset + 8, false) - this.Map.Sbsp.SbspInfoCache[this.SbspInfoIndex].Magic);
            }
            set
            {
                this.Map.WriteAt(this.Map.Sbsp.SbspInfoCache[this.SbspInfoIndex].StartOffset + 8, (int) (value + this.Map.Sbsp.SbspInfoCache[this.SbspInfoIndex].Magic), false);
            }
        }

        public int PaletteCount
        {
            get
            {
                long position = this.Map.Position;
                this.Map.ReadReflexiveAt(this.StartOffset + 0x80, this.Map.Sbsp[this.SbspInfoIndex, Tags.SearchType.Index].Magic);
                this.Map.Position += 8L;
                int num2 = this.Map.ReadInt32();
                this.Map.Position = position;
                return num2;
            }
        }

        public Palette[] Palettes
        {
            get
            {
                return this._palettes;
            }
            set
            {
                this._palettes = value;
            }
        }

        public int SbspInfoIndex
        {
            get
            {
                return this.__sbspInfoIndex;
            }
        }

        public int Size
        {
            get
            {
                return ((this.Map.Sbsp.SbspInfoCache[this.SbspInfoIndex].Offset + this.Map.Sbsp.SbspInfoCache[this.SbspInfoIndex].Size) - this.Offset);
            }
        }

        public string TagPath
        {
            get
            {
                return this.Map.Tags.GetTagPath(this.ID, Tags.SearchType.ID);
            }
        }

        public class Palette
        {
            private int _offset;
            private Bitmaps.RGBA_ByteColor[] ColorsCache;
            private MapStream Map;

            internal Palette(MapStream map, int offset)
            {
                this.Map = map;
                this.Offset = offset;
                this.CreateCache();
            }

            internal void CreateCache()
            {
                this.ColorsCache = new Bitmaps.RGBA_ByteColor[0x100];
                for (int i = 0; i < 0x100; i++)
                {
                    this.ColorsCache[i].R = this.Map.ReadByte();
                    this.ColorsCache[i].G = this.Map.ReadByte();
                    this.ColorsCache[i].B = this.Map.ReadByte();
                    this.ColorsCache[i].A = this.Map.ReadByte();
                }
            }

            public void Flush()
            {
                long position = this.Map.Position;
                this.Map.Position = this.Offset;
                for (int i = 0; i < 0x100; i++)
                {
                    this.Map.Write(this.ColorsCache[i].R);
                    this.Map.Write(this.ColorsCache[i].G);
                    this.Map.Write(this.ColorsCache[i].B);
                    this.Map.Write(this.ColorsCache[i].A);
                }
                this.Map.Position = position;
                this.Map.Flush();
            }

            public Bitmaps.RGBA_ByteColor[] Colors
            {
                get
                {
                    return this.ColorsCache;
                }
                set
                {
                    this.ColorsCache = value;
                }
            }

            public int Offset
            {
                get
                {
                    return this._offset;
                }
                set
                {
                    this._offset = value;
                }
            }
        }
    }
}

